package com.feihong.txgw.utils.greendao;

import android.support.annotation.NonNull;

import com.feihong.txgw.greendao.gen.ShopCarGoodsModelDao;
import com.feihong.txgw.json.goodsCart.GoodsCartParser;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.utils.UserLoginInfo;

import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName GreenDaoOperateHelper
 * @package com.feihong.txgw.utils.greendao
 * @data 2018/5/2 16:45
 * @Describe
 * @Change
 */
public class GreenDaoOperateHelper {
    /**
     * 添加一条
     *
     * @param model
     * @return
     */
    public static boolean addGoods(ShopCarGoodsModel model) {
        GreenDaoManager.getShopCarGoodsModelDao().insert(model);
        return true;
    }

    /**
     * 添加一个集合数据
     * @param models
     * @return
     */
    public static boolean addGoods(List<ShopCarGoodsModel> models) {
        GreenDaoManager.getShopCarGoodsModelDao().insertInTx(models);
        return true;
    }

    /**
     * 将传入类型 转换为可以购物车商品类型 并保存
     * @param models
     * @return
     */
    public static boolean addGoodsCast(List<GoodsCartParser.DataBean> models){
        ShopCarGoodsModel shopCarGoodsModel;
        for(GoodsCartParser.DataBean  dataBean : models){
            shopCarGoodsModel = createShopCarGoodsModel(dataBean);
            addGoods(shopCarGoodsModel);
        }
        return true;
    }
    /**
     * 获取所有商品
     * @return
     */
    public static List<ShopCarGoodsModel> getAllGoods() {
        List<ShopCarGoodsModel> modelList = GreenDaoManager.getShopCarGoodsModelDao().queryBuilder().build().list();
        return modelList;
    }

    /**
     * 更新数据库中的指定商品
     * @param shopcarGoodsModel
     * @return
     */
    public static void updateGoods(ShopCarGoodsModel shopcarGoodsModel) {
        List<ShopCarGoodsModel> list = isSaved(shopcarGoodsModel);
        if (list.size() > 0) {
            ShopCarGoodsModel newGoods = new ShopCarGoodsModel();
            newGoods.setGoodsName(shopcarGoodsModel.getGoodsName());
            newGoods.setGoodsID(shopcarGoodsModel.getGoodsID());
            newGoods.setAction(shopcarGoodsModel.getAction());
            newGoods.setAddTime(shopcarGoodsModel.getAddTime());
            newGoods.setGoodsAllNum(shopcarGoodsModel.getGoodsAllNum());
            newGoods.setGoodsNum(shopcarGoodsModel.getGoodsNum());
            newGoods.setGoodsPic(shopcarGoodsModel.getGoodsPic());
            newGoods.setGoodsPrice(shopcarGoodsModel.getGoodsPrice());
            newGoods.setGoodsSpec(shopcarGoodsModel.getGoodsSpec());
            newGoods.setGoodsStatus(shopcarGoodsModel.getGoodsStatus());
            newGoods.setGoodsSubtitle(shopcarGoodsModel.getGoodsSubtitle());
            newGoods.setIsCheck(shopcarGoodsModel.getIsCheck());
            newGoods.setIsUpdate(shopcarGoodsModel.getIsUpdate());
            newGoods.setPrimaryKey(shopcarGoodsModel.getPrimaryKey());
            newGoods.setSn(shopcarGoodsModel.getSn());
            newGoods.setUserID(shopcarGoodsModel.getUserID());

            newGoods.setId(list.get(0).getId());

            GreenDaoManager.getShopCarGoodsModelDao().update(newGoods);
        } else {
            GreenDaoManager.getShopCarGoodsModelDao().insert(shopcarGoodsModel);
        }
    }

    /**
     * 更新商品集
     * @param models
     */
    public static void updateGoods(List<ShopCarGoodsModel> models){
        for(ShopCarGoodsModel shopcarGoodsModel : models){
            updateGoods(shopcarGoodsModel);
        }
    }

    public static void updateDataBeanGoods(List<GoodsCartParser.DataBean> models){
        ShopCarGoodsModel shopCarGoodsModel;
        for(GoodsCartParser.DataBean dataBean: models){
            shopCarGoodsModel = createShopCarGoodsModel(dataBean);
            updateGoods(shopCarGoodsModel);
        }
    }

    /**
     * 转换数据类
     * @param dataBean
     * @return
     */
    @NonNull
    private static ShopCarGoodsModel createShopCarGoodsModel(GoodsCartParser.DataBean dataBean) {
        ShopCarGoodsModel shopCarGoodsModel;
        shopCarGoodsModel = new ShopCarGoodsModel();
        shopCarGoodsModel.setAddTime(System.currentTimeMillis());
        shopCarGoodsModel.setGoodsPrice(Float.parseFloat(dataBean.getShop_price()));
        shopCarGoodsModel.setGoodsAllNum(Integer.parseInt(dataBean.getNumber()));
        shopCarGoodsModel.setGoodsName(dataBean.getSku_name());
        shopCarGoodsModel.setGoodsID(Integer.parseInt(dataBean.getSku_id()));
        shopCarGoodsModel.setIsUpdate(1);
        shopCarGoodsModel.setGoodsSubtitle(dataBean.getSubtitle());
        shopCarGoodsModel.setGoodsPic(dataBean.getThumb());
        shopCarGoodsModel.setGoodsNum(Integer.parseInt(dataBean.getNums()));
        shopCarGoodsModel.setGoodsSpec(dataBean.getSpec());
        shopCarGoodsModel.setGoodsStatus(Integer.valueOf(dataBean.getStatus()));
        shopCarGoodsModel.setUserID(Long.parseLong(UserLoginInfo.getUserId()));
        return shopCarGoodsModel;
    }

    /**
     * 删除所有商品
     *
     * @return
     */
    public static boolean delAll() {
        GreenDaoManager.getShopCarGoodsModelDao().deleteAll();
        return true;
    }

    /**
     * 删除指定商品
     *
     * @param shopcarGoodsModel
     * @return
     */
    public static void delModel(ShopCarGoodsModel shopcarGoodsModel) {

        List<ShopCarGoodsModel> resultList = isSaved(shopcarGoodsModel);
        if (resultList.size() > 0) {
            GreenDaoManager.getShopCarGoodsModelDao().delete(shopcarGoodsModel);
        }
    }

    /**
     * 删除商品集合
     * @param shopcarGoodsModels
     * @return
     */
    public static boolean delModel(List<ShopCarGoodsModel> shopcarGoodsModels) {
        for (ShopCarGoodsModel shopcarGoodsModel : shopcarGoodsModels) {
            delModel(shopcarGoodsModel);
        }
        return true;
    }

    /**
     * 删除指定商品
     * @param shopcarGoodsModel
     */
    private void toDel(ShopCarGoodsModel shopcarGoodsModel) {
        List<ShopCarGoodsModel> shopcarGoodsModelList = getModel(ShopCarGoodsModelDao.Properties.GoodsID, String.valueOf(shopcarGoodsModel.getGoodsID()));
        GreenDaoManager.getShopCarGoodsModelDao().deleteByKey(shopcarGoodsModelList.get(0).getId());
    }

    /**
     * 获取指定商品
     *
     * @return
     */
    public static List<ShopCarGoodsModel> getModel(Property column, String value) {
        List<ShopCarGoodsModel> shopcarGoodsModelList = GreenDaoManager.getShopCarGoodsModelDao().queryBuilder().where(column.eq(value)).list();
        return shopcarGoodsModelList;
    }

    /**
     * 此条商品是否在数据库存在
     * @param shopcarGoodsModel
     * @return
     */
    private static List<ShopCarGoodsModel> isSaved(ShopCarGoodsModel shopcarGoodsModel) {
        QueryBuilder<ShopCarGoodsModel> queryBuilder = GreenDaoManager.getShopCarGoodsModelDao().queryBuilder();
        List<ShopCarGoodsModel> shopcarGoodsModelList = queryBuilder.where(ShopCarGoodsModelDao.Properties.GoodsID.eq(shopcarGoodsModel.getGoodsID())).list();
        return shopcarGoodsModelList;
    }
}
